package org.jeecg.modules.wlhy.bill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.paycenter.channel.bankdocking.service.NhBankService;
import org.jeecg.modules.wlhy.bill.entity.HyCustomerBill;
import org.jeecg.modules.wlhy.bill.entity.HyCustomerBillDTO;
import org.jeecg.modules.wlhy.bill.mapper.HyCustomerBillMapper;
import org.jeecg.modules.wlhy.bill.service.IHyCustomerBillService;
import org.jeecg.modules.wlhy.order.vo.HyOrderVO;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: hy_customer_bill
 * @Author: jeecg-boot
 * @Date:   2021-01-20
 * @Version: V1.0
 */
@Service
//@EnableAsync
public class HyCustomerBillServiceImpl extends ServiceImpl<HyCustomerBillMapper, HyCustomerBill> implements IHyCustomerBillService {
    @Autowired
    private NhBankService nhBankService;
    @Resource
    private IHyShippingNoteService shippingNoteService;

    @Override
    public String queryBankHdFu(HyCustomerBill hyCustomerBill){
        if (hyCustomerBill.getBankHdFu() != null){
            return hyCustomerBill.getBankHdFu();
        }
//        String shippingNoteNo = hyCustomerBill.getShippingId();
//        HyShippingNote shippingNote = shippingNoteService.getById(shippingNoteNo);
//        if(shippingNote == null){
//            throw new JeecgBootException("运单数据不存在");
//        }
        //付款 查本方账户明细 金额带负号
        String filename = nhBankService.queryBankHd(hyCustomerBill.getPayDt(), hyCustomerBill.getDbProv(), hyCustomerBill.getDbaccno(),
                hyCustomerBill.getBillNum(), hyCustomerBill.getAmt().negate());
        if(filename != null){
            hyCustomerBill.setBankHdFu(filename);
            this.updateById(hyCustomerBill);
        }
        return filename;
    }

    @Override
    public String queryBankHdShou(HyCustomerBill hyCustomerBill){
        if (hyCustomerBill.getBankHdShou() != null){
            return hyCustomerBill.getBankHdShou();
        }
        String shippingNoteNo = hyCustomerBill.getShippingId();
        HyShippingNote shippingNote = shippingNoteService.getById(shippingNoteNo);
        if(shippingNote == null){
            throw new JeecgBootException("运单数据不存在");
        }
        //收款 查对方账户明细 金额为正
        String filename = nhBankService.queryBankHd(hyCustomerBill.getPayDt(), hyCustomerBill.getCrProv(), hyCustomerBill.getCraccno(),
                hyCustomerBill.getBillNum(), hyCustomerBill.getAmt());
        if(filename != null){
            hyCustomerBill.setBankHdShou(filename);
            this.updateById(hyCustomerBill);
        }
        return filename;
    }

    @Override
    public boolean checkPayComplate(String PlatformCostid) {
        int count = this.count(
                new QueryWrapper<HyCustomerBill>()
                        .eq("platform_cost_id", PlatformCostid)
                        .ne("resp_source", "0")
        );
        return count == 0;
    }

    @Override
    public HyCustomerBill getPreBillByShippingId(String shipping_id, String customerId) {
        return this.getOne(new QueryWrapper<HyCustomerBill>()
                .eq("shipping_id", shipping_id)
                .eq("customer_id", customerId)
                .eq("bill_type", "6")
        );
    }

    @Override
    public HyCustomerBill getBillByShippingId(String shipping_id, String customerId) {
        return this.getOne(new QueryWrapper<HyCustomerBill>()
                .eq("shipping_id", shipping_id)
                .eq("customer_id", customerId)
//                .eq("bill_type", "1")
                .and(qw->qw.eq("bill_type", "1")
                        .or()
                        .eq("bill_type", "2")
                )
        );
    }

    @Override
    public List<HyCustomerBillDTO> getList(QueryWrapper<HyCustomerBillDTO> queryWrapper) {
        return baseMapper.getList(queryWrapper);
    }

    @Override
    public IPage<HyCustomerBillDTO> getPageList(Page<HyCustomerBillDTO> page, QueryWrapper<HyCustomerBillDTO> queryWrapper) {
        IPage<HyCustomerBillDTO> pageList = baseMapper.getPageList(page, queryWrapper);
        return pageList;
    }
}
